<?php

namespace app\model\goods;

use app\base\BaseModel;

/**
 * 模型-GoodsFreightModel
 * Class GoodsFreightModel
 * @package app\model
 */
class GoodsFreightModel extends BaseModel
{
    protected $name = 'goods_freight';

    /**
     * 店铺id 查询范围
     * @param $query
     * @param $id
     */
    public function scopeStoreId($query, $id)
    {
        if (!empty($id)) {
            $query->alias("a");
            $query->where('a.store_id', $id);
        }
    }

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('name', "%$keyword%");
        }
    }

    /**
     * 获取商品运费
     * @param string $id
     * @return array|bool|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getFreight($id = '')
    {
        $data = [
            'base_freight' => 0,
            'one_freight' => 0
        ];
        if (empty($id)) {
            return $data;
        }
        $data = self::where(['id' => $id])->find();
        return $data;
    }

    /**
     * 获取分页列表
     * @param $map
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $store_id
     * @return array
     */
    public function getPageData($map, $keyword = "", $current_page, $list_rows, $store_id = '')
    {
        $dataList = $this
            ->keyword($keyword)
            ->storeId($store_id)
            ->where($map)
            ->order('id', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 获取运费列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getFreightList()
    {
        $return_data = [];
        $data_list = self::select();
        if (!empty($data_list)) {
            foreach ($data_list as $value) {
                $return_data[$value['id']] = $value;
            }
        }
        return $return_data;
    }
}
